/**
 * 
 */
package com.dhcc.cdbp.score;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author 谢进军
 *
 */
public class FieldBootTest {

	private Directory directory = null;
	@Before public void setUp() throws Exception {
		directory = new RAMDirectory();
		IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
		IndexWriter writer = new IndexWriter(directory, conf);
		Document document = new Document();
		Field field1 = new TextField("title", "Java , hello world!", Store.YES);
		field1.setBoost(10);
		document.add( field1 );
		writer.addDocument(document);
		document = new Document();
		Field filed2 = new TextField("title", "Java ,I like it.", Store.YES);
		filed2.setBoost(100);
		document.add(filed2);
		writer.addDocument(document);
		writer.close();
	}
	@Test public void test() throws IOException {
		IndexReader reader = DirectoryReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(reader);
		Query query = new TermQuery(new Term("title", "java"));
		TopDocs hits = searcher.search(query, 10);
		ScoreDoc[] docs = hits.scoreDocs;
		if(null == docs || docs.length == 0) {
			System.out.println("No results for this query.");
			return;
		}
		for (ScoreDoc scoreDoc : docs) {
			int docID = scoreDoc.doc;
			float score = scoreDoc.score;
			Document document = searcher.doc(docID);
			String title = document.get("title");
			System.out.println("docId:" + docID);
			System.out.println("title:" + title);
			System.out.println("score:" + score);
			System.out.println("\n");
		}
		reader.close();
		directory.close();
	}
	@After public void tearDown() throws Exception {
	}
}
